package file;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class csv
{
	/** decypher csv and put key=values into a HashMap
	 * returns an HashMap of HashMaps that is structured like this:
	 * [0]{key=value1,key=value2}
	 * [1]{key=value3,key=value4}
	 * [2]{key=value5,key=value6}
	 * 
	 * if elements_per_line is given it will strictly "see" each line as amount of elements_per_line packs of values.
	 * **/
	public HashMap csv2HashMap(String data, String elements_per_line, String data_format, String first_line_contains_keys, String value_enclosed_by, String values_delimiter, String line_delimiter )
	{
		String VALUE = "";
		
		String KEY = "";
		Integer elements_per_line_int = Integer.parseInt(elements_per_line);
		HashMap RESULT = new HashMap();
		
		Pattern PATTERN1 = Pattern.compile(line_delimiter);
		Pattern PATTERN2 = Pattern.compile(values_delimiter);

		/** Split a String into a Java Array of Strings divided by an RE
		 */
		Integer KEY_COUNT = 0;
		
		// Map<Integer,String> KEYS = new HashMap<Integer,String>();
		HashMap NEW_LINE = new HashMap();

		// treat lines as pack of values
    	if(!elements_per_line.equals(""))
    	{
    		// split by line
    		String[] VALUES = PATTERN2.split(data);
    		ArrayList<String> KEYS = new ArrayList<String>();

    	    for (Integer counter=0; counter<VALUES.length; counter++)
    	    {
	    	    VALUE = VALUES[counter];

	    		if(counter < elements_per_line_int)
	    		{
	    			if(first_line_contains_keys.equals("true"))
	    			{
	    				// first line contains keys
	    				VALUE = VALUE.replace(value_enclosed_by, "");
	    				KEYS.add(VALUE);
	    			}
	    		}
	    		else
	    		{
	    			VALUE = VALUE.replace(value_enclosed_by, "");

    				if(!elements_per_line_int.equals(KEY_COUNT))
    				{
    					KEY = KEYS.get(KEY_COUNT);
	    			}

    				if(!KEY.equals(""))
	    			{
	    				if(elements_per_line_int.equals(KEY_COUNT))
	    				{
	    					RESULT.put(counter, NEW_LINE);
	    					NEW_LINE = new HashMap();
	    					KEY_COUNT = 0;
	    					KEY = KEYS.get(KEY_COUNT);
	    					NEW_LINE.put(KEY, VALUE);
	    					KEY_COUNT++;
	    				}
	    				else
	    				{
	    					NEW_LINE.put(KEY, VALUE);
	    					KEY_COUNT++;
	    					System.out.println(KEY+"="+VALUE);
	    				}
	    			}
				}
    	    }
    	}
    	else
    	{
    		// split lines by line_delimiter
    		// split by line
    	    String[] LINES = PATTERN1.split(data);
    	    String[] KEYS = null;
    	    String[] VALUES = null;
    		
    		for (Integer counter=0; counter<LINES.length; counter++)
    	    {
				// first line contains keys
				if(counter == 0)
				{
					if(first_line_contains_keys.equals("true"))
					{
						KEYS = PATTERN2.split(LINES[counter]);
					}
				}
				else
				{
					VALUES = PATTERN2.split(LINES[counter]);
					// removing value_enclosed_by
					for (Integer counter2=0; counter2<VALUES.length; counter2++)
					{
						VALUE = VALUES[counter2];
						VALUE = VALUE.replace(value_enclosed_by, "");
						try
						{
							KEY = KEYS[counter2];
							KEY = KEY.replace(value_enclosed_by, "");
							NEW_LINE.put(KEY, VALUE);
						}
						catch (Exception e)
						{
						}
						System.out.println(KEY+"="+VALUE);
					}
					RESULT.put(counter, NEW_LINE);
				}
    	    }
   	    }

//	    try
//		{
//		}
//    	catch (Exception e)
//    	{
//    		// TODO: handle exception
//    		String ERROR_MESSAGE = "ERROR WHILE PARSING CSV maybe CSV structure inconsistent? (too much too less delimiters?) "+e;
//    		System.out.println(ERROR_MESSAGE);
//    		RESULT = new HashMap();
//    		RESULT.put("ERROR", ERROR_MESSAGE);
//    	}
	    
	    return RESULT;
	}
}
